// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <nebula/types/fundamental.h>

namespace nebula {


    /// \brief Concrete type class for struct data
    class TURBO_EXPORT StructType : public NestedType {
    public:
        static constexpr Type::type type_id = Type::STRUCT;

        static constexpr const char *type_name() { return "struct"; }

        explicit StructType(const FieldVector &fields);

        ~StructType() override;

        DataTypeLayout layout() const override {
            return DataTypeLayout({DataTypeLayout::Bitmap()});
        }

        std::string to_string(bool show_metadata = false) const override;

        std::string name() const override { return "struct"; }

        /// Returns null if name not found
        std::shared_ptr<Field> get_field_by_name(const std::string &name) const;

        /// Return all fields having this name
        FieldVector get_all_fields_by_name(const std::string &name) const;

        /// Returns -1 if name not found or if there are multiple fields having the
        /// same name
        int get_field_index(const std::string &name) const;

        /// \brief Return the indices of all fields having this name in sorted order
        std::vector<int> get_all_field_indices(const std::string &name) const;

        /// \brief create a new StructType with field added at given index
        turbo::Result<std::shared_ptr<StructType>> add_field(int i,
                                                            const std::shared_ptr<Field> &field) const;

        /// \brief create a new StructType by removing the field at given index
        turbo::Result<std::shared_ptr<StructType>> remove_field(int i) const;

        /// \brief create a new StructType by changing the field at given index
        turbo::Result<std::shared_ptr<StructType>> set_field(int i,
                                                            const std::shared_ptr<Field> &field) const;

    private:
        std::string compute_fingerprint() const override;

        class Impl;

        std::unique_ptr<Impl> impl_;
    };

}  // namespace nebula
